package com.ohuang.myapp.fragment.one;

import com.ohuang.baseapp.mvp.BaseMvpFragment;
import com.ohuang.baseapp.utils.ToastUtils;
import com.ohuang.baseapp.view.refresh.OhRefrsh.OhRefreshLayout;
import com.ohuang.baseapp.view.refresh.OhRefrsh.OnRefreshListener;
import com.ohuang.baseapp.view.refresh.OhRefrsh.Refresh;
import com.ohuang.myapp.ResourceTable;
import com.ohuang.myapp.bean.WYNewsBean;
import com.ohuang.myapp.presenter.ListFgPresenter;
import com.ohuang.myapp.provider.NewsProvider;
import com.ohuang.myapp.view.IListFgView;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.LinkedList;
import java.util.List;

public class ListFragment extends BaseMvpFragment<ListFgPresenter> implements IListFgView {
    @Override
    protected int getXmlId() {
        return ResourceTable.Layout_fg_list;
    }

    OhRefreshLayout ohRefreshLayout;

    ListContainer listContainer;
    NewsProvider listProvider;
    List<WYNewsBean.ResultBean> list=new LinkedList<>();
    int page=1;
    private boolean lastVisible=false;
    private boolean isLoad=false;



    @Override
    protected void initComponent() {
       listContainer=  findComponentById(ResourceTable.Id_listview);
       ohRefreshLayout=findComponentById(ResourceTable.Id_refresh);
       mPresenter=new ListFgPresenter();
       mPresenter.attachView(this);
       listProvider=new NewsProvider(list,getContext());
       listContainer.setItemProvider(listProvider);
       listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
           @Override
           public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
              Intent intent=new Intent();
               Operation operation = new Intent.OperationBuilder()
                       .withBundleName("com.ohuang.myapp")
                       .withAbilityName("com.ohuang.myapp.WebAbility")
                       .build();

// 把operation设置到intent中
               intent.setOperation(operation);
               intent.setParam("url",list.get(i).getPath());
               getAbility().startAbility(intent);
           }
       });


//       ohRefreshLayout.setCanPullUp(false);
       ohRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
           @Override
           public void onRefresh(Refresh refresh) {
               mPresenter.LoadData(page++);

           }

           @Override
           public void onBottomRefresh(Refresh refresh) {
              mPresenter.LoadMore(page++);
           }
       });

    }

    @Override
    public void userVisibleChange(boolean isVisible) {
        if (!lastVisible&isVisible){
            loadData();
        }else {

        }

        lastVisible=isVisible;
    }

    private void loadData(){
        if (!isLoad){
            mPresenter.LoadData(page++);
            isLoad=true;
        }else {
            
        }
    }


    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    public void LoadData(WYNewsBean wyNewsBean) {

        list.addAll(0,wyNewsBean.getResult());
        listProvider.notifyDataChanged();


    }

    @Override
    public void ShowToast(String s) {
        ToastUtils.showToastDialog(getContext(),s);
    }

    @Override
    public void refreshOk() {
        ohRefreshLayout.onRelease();
    }

    @Override
    public void LoadMoreData(WYNewsBean wyNewsBean) {
        list.addAll(wyNewsBean.getResult());
        listProvider.notifyDataChanged();
    }
}
